package com.iplatform.file.function.resourcedata.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iplatform.commons.pagination.Pagination;
import com.iplatform.commons.utils.CommResponse;

import com.iplatform.file.function.resourcedata.entity.FileBeanDto;
import com.iplatform.file.function.resourcedata.entity.FileBean;
import com.iplatform.file.function.resourcedata.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @className: FileController
 * @description: TODO 类描述
 * @author: chensk
 * @date: 2020/12/3
 **/
@RestController
@RequestMapping("/file")
@Api(tags = "文件模块接口")
public class FileController {
    @Resource
    private FileService fileService;



    @PostMapping(value = "/addFile")
    @ApiOperation(value = "文件新增操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：addFile")
    @ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
            @ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
    public ResponseEntity<CommResponse<Integer>> addFile(@RequestBody FileBeanDto fileBean) {
        Integer result = fileService.insert(fileBean);
        return new ResponseEntity<>(new CommResponse<Integer>(HttpStatus.CREATED.toString(), Boolean.TRUE,result), HttpStatus.CREATED);

    }



    @RequestMapping(value="/selectByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "文件查询操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：selectByCondition")
    @ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
            @ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
    public ResponseEntity<PageInfo<FileBean>> selectByCondition(Long parentId,String fileName, Pagination pagination) {
        PageHelper.startPage(pagination.getPageNum(),pagination.getPageSize());
        List<FileBean> result= fileService.selectByCondition(parentId,fileName);
        PageInfo<FileBean> pageInfo = new PageInfo<>(result);
        return new ResponseEntity<>(pageInfo,HttpStatus.OK);
    }

   @RequestMapping(value="/removeById", method = RequestMethod.DELETE)
    @ApiOperation(value = "根据ID删除操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：removeById")
    @ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
            @ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
    public ResponseEntity<CommResponse<Integer>> removeById(Long id) {
       Integer result = fileService.removeById(id);
       return new ResponseEntity<>(new CommResponse<Integer>(HttpStatus.NO_CONTENT.toString(), Boolean.TRUE,result), HttpStatus.NO_CONTENT);

   }
    
    @RequestMapping(value="/selectById", method = RequestMethod.GET)
    @ApiOperation(value = "文件ID查询操作", notes = "所在包：com.iplatform.data.function.resource.controller 方法名：selectById")
    @ApiResponses({@ApiResponse(code = HttpServletResponse.SC_OK, message = "成功"),
            @ApiResponse(code = HttpServletResponse.SC_CONFLICT, message = "没有做到API使用规范")})
    public ResponseEntity<CommResponse<FileBean>> selectById(Long id) {

        FileBean result = fileService.selectDatabyId(id);

        CommResponse<FileBean> response = new CommResponse<>();
        response.setMsg(HttpStatus.OK.toString());
        response.setRes(Boolean.TRUE);
        response.setResult(result);
        return new ResponseEntity<>(response,HttpStatus.OK);
    }
}
